package com.lenovo.manufacture.Fragment;


import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.lenovo.manufacture.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.POST;

/**
 * A simple {@link Fragment} subclass.
 */
public class Rencai extends Fragment {
private  Myadpter myadpter=new Myadpter();
    @BindView(R.id.Rencai_tv1)
    TextView RencaiTv1;
    @BindView(R.id.Rencai_tv2)
    TextView RencaiTv2;
    @BindView(R.id.Rencai_listview)
    ListView RencaiListview;
    private List<Rencaishujv> list = new ArrayList<>();
    private int r=0 ,q=0;
    public Rencai() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_rencai, container, false);
        ButterKnife.bind(this, inflate);
        inst();
        return inflate;
    }

    @OnClick({R.id.Rencai_tv1, R.id.Rencai_tv2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.Rencai_tv1:
               r+=1;
               if (r==1){
                   RencaiTv1.setText("类型 ↑");
                   Log.d("TAG", "onViewClicked: "+r);
                   Collections.sort(list.get(0).getData(), new Comparator<Rencaishujv.DataBean>() {
                       @Override
                       public int compare(Rencaishujv.DataBean o1, Rencaishujv.DataBean o2) {
                           return o1.getType()-o2.getType();
                       }
                   });
                   myadpter.notifyDataSetChanged();
               }else if (r==2){
                   RencaiTv1.setText("类型 ↓");
                   Log.d("TAG", "onViewClicked: "+r);

                   Collections.sort(list.get(0).getData(), new Comparator<Rencaishujv.DataBean>() {
                       @Override
                       public int compare(Rencaishujv.DataBean o1, Rencaishujv.DataBean o2) {
                           return o2.getType()-o1.getType();
                       }
                   });
                   r=0;
                   myadpter.notifyDataSetChanged();
               }
                break;
            case R.id.Rencai_tv2:
                q+=1;
                if (q==1){
                    RencaiTv2.setText("薪资 ↑");
                    Collections.sort(list.get(0).getData(), new Comparator<Rencaishujv.DataBean>() {
                        @Override
                        public int compare(Rencaishujv.DataBean o1, Rencaishujv.DataBean o2) {
                            return o1.getPrice()-o2.getPrice();
                        }
                    });
                    myadpter.notifyDataSetChanged();
                }else if (q==2){
                    RencaiTv2.setText("薪资 ↓");
                    Collections.sort(list.get(0).getData(), new Comparator<Rencaishujv.DataBean>() {
                        @Override
                        public int compare(Rencaishujv.DataBean o1, Rencaishujv.DataBean o2) {
                            return o2.getPrice()-o1.getPrice();
                        }
                    });
                    q=0;
                    myadpter.notifyDataSetChanged();
                }
                break;
        }
    }

    public interface Bolg {
        @POST("Interface/Person/getAll")
        Call<ResponseBody> getbolg();
    }

    public void inst() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://hh1.me:5001/").build();
        Bolg bolg = retrofit.create(Bolg.class);
        Call<ResponseBody> call = bolg.getbolg();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Gson gson = new Gson();
                try {
                    Rencaishujv rencaishujv = gson.fromJson(response.body().string(), Rencaishujv.class);
                    list.add(rencaishujv);

                    RencaiListview.setAdapter(myadpter);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    public class Myadpter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.get(0).getData().size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView==null){
                convertView = View.inflate(getContext(), R.layout.recai_list, null);
                viewHolder=new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }else {
                viewHolder= (ViewHolder) convertView.getTag();
            }
            viewHolder.recaiListTv1.setText(list.get(0).getData().get(position).getPersonName()+"");
            switch (list.get(0).getData().get(position).getType()){
                case 0:
                    viewHolder.recaiListTv2.setText("工程师");
                    break;
                case 1:
                    viewHolder.recaiListTv2.setText("工人");
                    break;
                case 2:
                    viewHolder.recaiListTv2.setText("技术人员");
                    break;
                case 3:
                    viewHolder.recaiListTv2.setText("质检员");
                    break;
            }
            viewHolder.recaiListTv3.setText(list.get(0).getData().get(position).getPrice()+"");
            return convertView;
        }

        class ViewHolder {
            @BindView(R.id.recai_list_tv1)
            TextView recaiListTv1;
            @BindView(R.id.recai_list_tv2)
            TextView recaiListTv2;
            @BindView(R.id.recai_list_tv3)
            TextView recaiListTv3;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
